import re
import requests
from bs4 import BeautifulSoup

txtUserID = int(input('Username:'))
txtPasswd = input('Password:')

login_URL = 'http://222.30.63.15/NKEMIS/SystemLogin.aspx'

headers = {
	"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
	"Accept-Encoding": "gzip, deflate",
	"Accept-Language": "zh-CN,zh;q=0.8",
	"Cache-Control": "max-age=0",
	"Connection": "keep-alive",
	"Content-Type": "application/x-www-form-urlencoded",
	"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"
}
# 获取CSRF验证
response = requests.get(login_URL, headers=headers)
bsObj = BeautifulSoup(response.text, 'lxml')
__VIEWSTATE = bsObj.find('input').attrs['value']
__EVENTVALIDATION = bsObj.find('input', id='__EVENTVALIDATION').attrs['value']

from_data = {
	"__VIEWSTATE": __VIEWSTATE,
	"__EVENTVALIDATION": __EVENTVALIDATION,
	"txtUserID": txtUserID,
	"txtPasswd": txtPasswd,
	"ImageButton1.x": 1,
	"ImageButton1.y": 1,
}

session = requests.session()
post = session.post(login_URL, data=from_data)
index_url = 'http://222.30.63.15/NKEMIS/Student/StudentDefault.aspx'
index_html = session.get(index_url)  # 成功抓取
# print(index_html)
tbody_bsObj = BeautifulSoup(index_html.text, 'lxml')
# 课程相关标题
tr = tbody_bsObj.find('tr', {'class':'dgrdHeader'}).find_all('td')
for title, students_number, homework in zip(tr[0], tr[1], tr[-1]):
	print(title + '\t\t', students_number + '\t\t', homework + '\t\t')

all_title = tbody_bsObj.find_all('a', style="color:red")
for title in all_title:
    print(title.get_text().strip())